package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.PageParam;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/category")
public class CategoryController {


    @Autowired
    private CategoryService cs;

    /*更新分类*/
    @PutMapping
    private R update(@RequestBody Category category) {
        int row = cs.update(category);
        if (row > 0){
            return R.success("更新成功！");
        }else {
            return R.error("更新失败！");
        }
    }

    /*删除分类*/
    @DeleteMapping
    private R delete(long id) {

        int row = cs.delete(id);

        if (row > 0) {
            return R.success("删除分类成功！");
        } else {
            return R.error("删除分类失败！");
        }

    }

    /*分页查询*/
    @GetMapping("/page")
    public R findPage(PageParam pageParam) {

        try {
            IPage<Category> page = cs.findPage(pageParam);

            return R.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("分页查询失败");
        }

    }

    /*添加分类*/
    @PostMapping
    public R add(@RequestBody Category category) {
        int row = cs.add(category);

        if (row > 0) {
            return R.success("添加分类成功!");
        } else {
            return R.error("添加分类失败！");
        }
    }


}
